﻿using AvalonDockSample.Views;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Controls;

namespace AvalonDockSample.ViewModels
{
    public class DocumentContainerViewModel : DocumentViewModel
    {
        private string _view;
        private IRegionManager _regionManager { get; set; }
        public DocumentContainerViewModel(IRegionManager regionManager,string view)
        {
            _view = view;
            _regionManager = regionManager.CreateRegionManager();
            OnLoadedCommand = new DelegateCommand<object>(OnLoaded);
        }

        private void OnLoaded(object obj)
        {
            if (obj == null)
            {
                return;
            }

            var userContrrol = (UserControl)obj;

            var documentRegion = "DocumentRegion";
            var documentContainerView = (UserControl)obj;
            if (!_regionManager.Regions.ContainsRegionWithName(documentRegion))
            {
                RegionManager.SetRegionManager(documentContainerView, _regionManager);
                RegionManager.UpdateRegions();
            }
            if (_regionManager.Regions.ContainsRegionWithName(documentRegion))
            {
                _regionManager.Regions[documentRegion].RequestNavigate(_view);
            }
        }

        public DelegateCommand<object> OnLoadedCommand { get; set; }
    }
}
